package org.je.util;

/**
 * @author fanyifeng 2012-8-31 上午10:05:19
 */
public class NumberUtil
{
	/**
	 * int 可以直接转byte 截取第1个字节
	 * 
	 * @param n
	 * @return bytes
	 */
	public static byte[] int2bytes(int n) {
		byte[] b = new byte[4];
		b[0] = (byte) n;
		b[1] = (byte) (n >>> 8);
		b[2] = (byte) (n >>> 16);
		b[3] = (byte) (n >>> 24);
		return b;
	}

	/**
	 * byte 需要 & 0xff才能强制转成int
	 * 
	 * @param b
	 * @return int number
	 */
	public static int bytes2int(byte[] b) {
		int n = 0;
		n |= b[0] & 0xff;
		n |= (b[1] & 0xff) << 8;
		n |= (b[2] & 0xff) << 16;
		n |= (b[3] & 0xff) << 24;
		return n;
	}

	public static int bytes2int(byte[] b, int off) {
		int n = 0;
		n |= b[off] & 0xff;
		n |= (b[off + 1] & 0xff) << 8;
		n |= (b[off + 2] & 0xff) << 16;
		n |= (b[off + 3] & 0xff) << 24;
		return n;
	}

	/**
	 * @param n
	 * @return byte[]
	 */
	public static byte[] long2bytes(long n) {
		byte[] b = new byte[8];
		b[0] = (byte) n;
		b[1] = (byte) (n >>> 8);
		b[2] = (byte) (n >>> 16);
		b[3] = (byte) (n >>> 24);
		b[4] = (byte) (n >>> 32);
		b[5] = (byte) (n >>> 40);
		b[6] = (byte) (n >>> 48);
		b[7] = (byte) (n >>> 56);
		return b;
	}

	/**
	 * @param b
	 * @return long
	 */
	public static long bytes2long(byte[] b) {
		long n = 0;
		n |= b[0] & 0xffL;
		n |= (b[1] & 0xffL) << 8;
		n |= (b[2] & 0xffL) << 16;
		n |= (b[3] & 0xffL) << 24;
		n |= (b[4] & 0xffL) << 32;
		n |= (b[5] & 0xffL) << 40;
		n |= (b[6] & 0xffL) << 48;
		n |= (b[7] & 0xffL) << 56;
		return n;
	}

	/**
	 * @param b
	 * @return long
	 */
	public static long bytes2long(byte[] b, int off) {
		long n = 0;
		n |= b[0 + off] & 0xffL;
		n |= (b[1 + off] & 0xffL) << 8;
		n |= (b[2 + off] & 0xffL) << 16;
		n |= (b[3 + off] & 0xffL) << 24;
		n |= (b[4 + off] & 0xffL) << 32;
		n |= (b[5 + off] & 0xffL) << 40;
		n |= (b[6 + off] & 0xffL) << 48;
		n |= (b[7 + off] & 0xffL) << 56;
		return n;
	}

	/**
	 * @param n
	 * @return bytes
	 */
	public static byte[] short2bytes(short n) {
		byte[] b = new byte[2];
		b[0] = (byte) n;
		b[1] = (byte) (n >>> 8);
		return b;
	}

	/**
	 * @param b
	 * @return short
	 */
	public static short bytes2short(byte[] b) {
		short n = 0;
		n |= b[0] & 0xff;
		n |= (b[1] & 0xff) << 8;
		return n;
	}

	/**
	 * @param b
	 * @param off
	 * @return short
	 */
	public static short bytes2short(byte[] b, int off) {
		short n = 0;
		n |= b[off] & 0xff;
		n |= (b[1 + off] & 0xff) << 8;
		return n;
	}

	/**
	 * @param a
	 * @param b
	 * @return min
	 */
	public static int min(int a, int b) {
		return a < b ? a : b;
	}

	/**
	 * @param a
	 * @param b
	 * @return max
	 */
	public static int max(int a, int b) {
		return a > b ? a : b;
	}

	/**
	 * @param a
	 * @param b
	 * @return a-b or b-a
	 */
	public static int abs(int a, int b) {
		return a > b ? a - b : b - a;
	}

	/**
	 * @param n
	 * @return
	 */
	public static int abs(int n) {
		return n < 0 ? (n * -1) : n;
	}
}
